#!/usr/bin/env bash

set -o errexit  #>Exit when a command fails (returns non-zero)
set -o pipefail #>Exit when a command within a pipeline fail (returns non-zero)
set -o nounset  #>Forbid to use unse

declare -A Paths
Paths[sys]="$HOME"/lognconf_systeminfo.md
Paths[log]="$HOME"/lognconf_logging.md
Paths[plot]="$HOME"/lognconf_plot.svg
Paths[url]="https://notabug.org/megavolt/random-scripts/raw/master/lognconf-report.sh"

if [[ ! -t 0 ]] ; then 
	echo "Pipe detected!"
	echo "Please don't pipe the script. Run it like that:"
	echo "* bash <(curl -s "${Paths[url]}")"
	echo "* curl -s ${Paths[url]} > /tmp/lncr.sh && bash /tmp/lncr.sh"
	echo "* bash lognconf-report.sh"
	echo "* chmod +x lognconf-report.sh && ./lognconf-report.sh"
	exit
fi

export LANG=C 

declare -A BootConfigs
BootConfigs[grub]='/etc/default/grub'
BootConfigs[mkinitcpio]='/etc/mkinitcpio.conf'
BootConfigs[fstab]='/etc/fstab'
BootConfigs[cryptab]='/etc/cryptab'
BootConfigs[xorg]='/etc/X11/xorg.conf'
BootConfigs[mkinitcpiod]='/etc/mkinitcpio.d/'
BootConfigs[xorgconfd]='/etc/X11/xorg.conf.d/'
BootConfigs[modeprobed]='/etc/modprobe.d/'
BootConfigs[modulesloadd]='/etc/modules-load.d/'

declare -A BootLogs
BootLogs[xorgsys]='/var/log/'
BootLogs[xorghme]="$HOME/.local/share/xorg/"

OutPut=""

function GetINXI ()
{
	if [[ -x /usr/bin/inxi ]]; then
		inxi --full --admin --filter --width 80 --color=0
	fi
}

function MHWDGetGPU ()
{
	local choice=$1
	local MHWD='/usr/bin/mhwd'
	local RMCOLOR='s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'
	if [[ -x $MHWD ]]; then
		if   [[ $choice == "list" ]]; then
			$MHWD --list | sed -r "${RMCOLOR}"
		elif [[ $choice == "installed" ]]; then
			$MHWD --listinstalled | sed -r "${RMCOLOR}"
		fi
	fi

}

function MHWDGetKernel ()
{
	local choice=$1
	local KRNL='/usr/bin/mhwd-kernel'
	local RMCOLOR='s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g'
	if [[ -x $KRNL ]]; then
		if   [[ $choice == "list" ]]; then
			$KRNL --list | sed -r "${RMCOLOR}"
		elif [[ $choice == "installed" ]]; then
			$KRNL --listinstalled | sed -r "${RMCOLOR}"
		fi
	fi
}

function GetXorgLogs ()
{
	if [[ $DESKTOP_SESSION == "gnome" ]]; then
		echo -e "\n#### XORG Journal (Gnome)\n\n~~~\n$(journalctl --boot 0 --user --grep "\([A-Z][A-Z]\)" --no-hostname --no-pager)\n~~~\n\n"
	fi
	if [[ $DESKTOP_SESSION != "gnome" ]]; then
		for index in "${!BootLogs[@]}"; do
			for path in $(find "${BootLogs[$index]}" -name "Xorg.*" 2>/dev/null); do
				echo -e "\n#### ${path}\n\n~~~\n$(cat "${path}")\n~~~\n\n"
			done
		done
	fi
}

function GetBootConfigs ()
{
	for index in "${!BootConfigs[@]}"; do
		# Search folders and get content
		if [[ ${index} == "xorgconfd" ]] \
		|| [[ ${index} == "modeprobed" ]] \
		|| [[ ${index} == "modulesloadd" ]] \
		|| [[ ${index} == "mkinitcpiod" ]]; then 
			for path in $(find "${BootConfigs[$index]}" \( -name "*.conf" -o -name "*.preset" \) 2>/dev/null); do
				echo -e "\n#### ${path}\n\n~~~\n$(cat "${path}")\n~~~\n\n"
			done
			continue
		fi
		if [[ -f "${BootConfigs[$index]}" ]]; then
			echo -e "\n#### ${BootConfigs[$index]}\n\n~~~\n$(cat "${BootConfigs[$index]}")\n~~~\n\n"
			
		fi
	done
}

function GetBlockDevices ()
{
	if [[ -x "/usr/bin/lsblk" ]]; then
		lsblk --fs --all
	fi
}

function GetSystemJournals () 
{
	local choice=$1
	if [[ -x "/usr/bin/journalctl" ]]; then
		if   [[ $choice == "current" ]]; then
			journalctl --boot 0 --no-hostname --no-pager
		elif [[ $choice == "previous" ]]; then
			journalctl --boot -1 --no-hostname --no-pager
		fi
	fi
}

function GetSystemLoadTime ()
{
	local choice=$1
	if [[ -x "/usr/bin/systemd-analyze" ]]; then
		if   [[ $choice == "svg" ]]; then
			systemd-analyze plot
		elif [[ $choice == "blame" ]]; then
			systemd-analyze blame
		elif [[ $choice == "basic" ]]; then
			systemd-analyze
		fi
	fi
}

function upload ()
{
	echo "Systeminfo: $(echo -e "${OutPut}" | curl -s -F'file=@-' https://0x0.st)"
	echo "Logs: $(echo -e "${Logs}" | curl -s -F'file=@-' https://0x0.st)"
	echo "Plot: $(echo -e "${Plot}" | curl -s -F'file=@-' https://0x0.st)"
}

function save ()
{
	echo -e "${OutPut}" > "${Paths[sys]}"
	echo -e "${Logs}" > "${Paths[log]}"
	echo -e "${Plot}" > "${Paths[plot]}"
	#cp /tmp/plot.svg "${Paths[plot]"
	echo "The output was saved here:"
	echo -e "Systeminfo -> ${Paths[sys]}"
	echo -e "Logs -> ${Paths[log]}"
	echo -e "Plot -> ${Paths[plot]}"

}

echo "Collecting INXI output..."
InPut="$(GetINXI)"
[[ -n "${InPut}" ]] && OutPut+="\n### INXI\n\n~~~\n${InPut}\n~~~\n\n"

echo "Collecting System Load Time output..."
InPut="$(GetSystemLoadTime basic)"
[[ -n "${InPut}" ]] && OutPut+="\n### System Load Time\n\n~~~\n${InPut}\n~~~\n\n"
InPut="$(GetSystemLoadTime blame)"
[[ -n "${InPut}" ]] && OutPut+="\n#### Blamed Services\n\n~~~\n${InPut}\n~~~\n\n"
Plot=$(GetSystemLoadTime svg)

echo "Collecting Manjaro Hardware Detection output..."
# Graphics
InPut="$(MHWDGetGPU list)"
[[ -n "${InPut}" ]] && OutPut+="\n### Manjaro Hardware Detection\n\n~~~\n${InPut}\n~~~\n\n"
InPut="$(MHWDGetGPU installed)"
[[ -n "${InPut}" ]] && OutPut+="\n\n~~~\n${InPut}\n~~~\n\n"
# Kernel
InPut="$(MHWDGetKernel list)"
[[ -n "${InPut}" ]] && OutPut+="\n\n~~~\n${InPut}\n~~~\n\n"
InPut="$(MHWDGetKernel installed)"
[[ -n "${InPut}" ]] && OutPut+="\n\n~~~\n${InPut}\n~~~\n\n"

echo "Collecting Blockdevices..."
InPut="$(GetBlockDevices)"
[[ -n "${InPut}" ]] && OutPut+="\n\n### BLOCKDEVICES\n\n~~~\n${InPut}\n~~~\n\n"

echo "Collecting Boot Configuration Files..."
InPut="$(GetBootConfigs)"
[[ -n "${InPut}" ]] && OutPut+="\n\n### Boot Configuration Files\n\n${InPut}\n\n"

Logs=""

echo "Collecting Xorg Logging Files..."
InPut="$(GetXorgLogs)"
[[ -n "${InPut}" ]] && Logs+="\n\n### Xorg Logging Files\n\n${InPut}\n\n"

echo "Collecting System Journals..."
InPut="$(GetSystemJournals current)"
[[ -n "${InPut}" ]] && Logs+="\n\n### System Journals"
[[ -n "${InPut}" ]] && Logs+="\n\n#### Current System Journals\n\n~~~\n${InPut}\n~~~\n\n"
InPut="$(GetSystemJournals previous)"
[[ -n "${InPut}" ]] && Logs+="\n\n#### Previous System Journals\n\n~~~\n${InPut}\n~~~\n\n"

read -p "Would you like to view [v] the content? (View[V]/No[n]) " vn
case $vn in
	[Vv]) less -f <(echo -e "${OutPut}${Logs}") ;;
	[Nn]) echo -n "" ;;
	*) less -f <(echo -e "${OutPut}${Logs}") ;;
esac

read -p "Would you like to upload [u] the content or write it to a file [s]? Save[s]/Upload[U]" us
case $us in
	[Uu]) upload ;;
	[Ss]) save ;;
	*) upload ;;
esac

